package com.fenfen.novel.utils;

import android.util.Log;

import com.fenfen.novel.model.Chapter;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class NovelFileUtils {

  private static String Tag = NovelFileUtils.class.getSimpleName();

  public static String CHAPTER_TITLE_PREFIX = "章节名：";

  private NovelFileUtils() {
  }

  public ArrayList<Chapter> convertChapterTitleList(ArrayList<String> chapterTitles, int novelId) {
    ArrayList<Chapter> chapterList = new ArrayList<>();
    if (chapterTitles != null && chapterTitles.size() > 0) {
      int order = 0;
      for (String title : chapterTitles) {
        order++;
        Chapter chapter = new Chapter();
        chapter.setNovelId(novelId);
        chapter.setChapterOrder(order);
        chapter.setChapterTitle(title);
        chapterList.add(chapter);
      }
    }
    return chapterList;
  }


  private static class INSTANCE {
    private static final NovelFileUtils instance = new NovelFileUtils();
  }

  public static NovelFileUtils getInstance() {
    return INSTANCE.instance;
  }


  public void getFileChapterList(final String path, final NovelFileCallbackListener listener) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        ArrayList<String> chapterTitles = getFileChapterList(path);
        if (listener != null) {
          listener.onChapterListFinish(chapterTitles);
        }
      }
    }).start();
  }

  public void getFileChapterContent(final String path, final int chapterOrder, final NovelFileCallbackListener listener) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        String[] result = getFileChapterContent(path, chapterOrder);
        if (listener != null) {
          String chapterContent = result[0];
          String title = result[1];
          listener.onChapterContentFinish(chapterContent, title);
        }
      }
    }).start();
  }


  /**
   * 获取txt文件中章节列表
   *
   * @param path txt文件地址
   */
  private ArrayList<String> getFileChapterList(String path) {
    ArrayList<String> chapterTitles = new ArrayList<>();
    BufferedReader br = null;
    try {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf8"));
      String lineStr;
      while ((lineStr = br.readLine()) != null) {
        if (lineStr.startsWith(CHAPTER_TITLE_PREFIX) || chapterTitles.size() == 0) {
          chapterTitles.add(lineStr.replace(CHAPTER_TITLE_PREFIX, ""));
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return chapterTitles;
  }

  private String[] getFileChapterContent(String path, int chapterOrder) {
    //读取文件的第chapterOrder个章节名和第chapterOrder+1个章节名之间内容
    StringBuffer content = new StringBuffer();
    String title = null;
    FileInputStream fis;
    BufferedReader br = null;
    try {
      fis = new FileInputStream(path);
      br = new BufferedReader(new InputStreamReader(fis, "utf8"));
      String lineStr;
      int index = 0;
      while ((lineStr = br.readLine()) != null) {
        if (lineStr.startsWith(CHAPTER_TITLE_PREFIX) || index == 0) {
          index++;
          if (index == chapterOrder) {
            title = lineStr.replace(CHAPTER_TITLE_PREFIX, "");
            Log.d(Tag, "get the right chapter[title=" + title + "],continue");
            continue;
          }
        }
        if (index == chapterOrder) {
          content.append(lineStr).append("\n");
        }
        if (index == chapterOrder + 1) {
          Log.d(Tag, "get content over");
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) {
          br.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return new String[]{content.toString(), title};
  }
}
